package com.aoip.telemetry.telemetry.analysis.decode;

import com.aoip.telemetry.telemetry.core.config.ElementAnalysisConfig;
import com.aoip.telemetry.telemetry.entity.element.ElementAdditionFiledConfig;
import com.aoip.telemetry.telemetry.entity.element.ElementMessageEntity;
import com.aoip.telemetry.telemetry.entity.common.TableDataEntity;
import com.aoip.telemetry.telemetry.entity.sn.SnHydrologyEntity;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.*;

/**
 * @author chenbai
 * @version 1.0
 * @date 2021/11/30 14:55
 */
@Component
public class SnHydrologyElementDataSaveDb {
    @Resource
    private ElementAnalysisConfig config;

    public List<TableDataEntity> saveElementData(List<ElementMessageEntity> list, SnHydrologyEntity entity) {
        List<Map<String, Object>> entityList = new ArrayList<>();
        String stCode = entity.getStCode();
        LocalDateTime observationTime = entity.getObservationTime();
        Set<String> tableSet = new HashSet<>();
        List<ElementAdditionFiledConfig> fieldExtConfigList = config.getFieldExtConfigList();
        for (ElementMessageEntity item : list) {
            String tableName = item.getTableName();
            if (StringUtils.hasLength(tableName)) {
                String filedName = item.getFiledName();
                Object value = item.getValue();
                Map<String, Object> map = new HashMap<>(4);
                tableSet.add(tableName);
                map.put("tableName", tableName);
                map.put("fieldName", filedName);
                map.put("value", value);
                map.put("stCode", stCode);
                entityList.add(map);
            }
        }
        fieldExtConfigList.forEach(vo -> {
            String tableName = vo.getTableName();
            tableSet.forEach(t -> {
                if (tableName.equalsIgnoreCase(t)) {
                    Map<String, Object> map = new HashMap<>(4);
                    String fieldName = vo.getFieldName();
                    String telemetryFieldName = vo.getTelemetryFieldName();
                    map.put("tableName", tableName);
                    map.put("fieldName", fieldName);
                    switch (telemetryFieldName) {
                        case "stCode":
                            map.put("value", stCode);
                            break;
                        case "tm":
                            if (observationTime != null) {
                                map.put("value", observationTime);
                            } else {
                                map.put("value", "");
                            }
                            break;
                        default:
                            break;
                    }
                    entityList.add(map);
                }
            });
        });
       return sortData(entityList);
    }

    private List<TableDataEntity> sortData(List<Map<String, Object>> entityList) {
        List<TableDataEntity> dataList = new ArrayList<>();
        Set<String> tableSet = new HashSet<>();
        for (Map<String, Object> vo : entityList) {
            TableDataEntity tableDataEntity = new TableDataEntity();
            String tableName = (String) vo.get("tableName");
            String fieldName = (String) vo.get("fieldName");
            Object value = vo.get("value");
            Map<String, Object> fieldMap = new HashMap<>(6);
            if (tableSet.contains(tableName)) {
                for (TableDataEntity item : dataList) {
                    if (tableName.equals(item.getTableName())) {
                        fieldMap = item.getFieldMap();
                        break;
                    }
                }
            } else {
                tableDataEntity.setStCode(vo.get("stCode").toString());
                dataList.add(tableDataEntity);
                tableDataEntity.setTableName(tableName);
                tableSet.add(tableName);
            }
            fieldMap.put(fieldName, value);
            tableDataEntity.setFieldMap(fieldMap);
        }
        return dataList;
    }
}
